package cn.sx.util;

/*
 *@program:sx_emall_ssm
 *@author: sx
 *@desc: mybatis工具类
 *@Time: 2020/9/17  10:29
 */

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisUtil {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try {
            /*
             * 使用mybatis第一步
             * 获取SqlSessionFactory对象
             * */
            String resource= "spring-mybatis.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    /*/既然有了SqlSessionFactory. 顾名思义。我们就可M从中获得SqLSession的实例了。
          SqlSession完全包含1面向数据库执行sQL命令所需的所有方法
          */
    public static SqlSession getSession(){
        return sqlSessionFactory.openSession();
    }
}
